package com.onenine.app.activity;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.onenine.app.R;
import com.onenine.app.event.RefreshEvent;
import com.onenine.app.model.GameCategory;
import com.onenine.app.request.AddGameRoomRequest;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.app.utils.UrlConfig;
import com.onenine.app.view.GameOptionView;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.okhttp.BaseResponse;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.base.utils.Toasty;
import com.onenine.chat.rongcloud.RcUser;

import org.greenrobot.eventbus.EventBus;

public class GameCreateActivity extends BaseActivity {

    private EditText mEdtIntroduce;
    private GameOptionView mGameOptionView;
    private Button mBtnCreate;

    @Override
    protected int getLayout() {
        return R.layout.activity_game_create;
    }

    private void setWindowTheme() {
        StatusBarUtils.setTransparent(this);
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.width = WindowManager.LayoutParams.MATCH_PARENT;
        wl.height = WindowManager.LayoutParams.MATCH_PARENT;
    }

    @Override
    protected void initViews() {
//        setWindowTheme();
        super.initViews();
        mEdtIntroduce = findViewById(R.id.edt_introduce);
        mGameOptionView = findViewById(R.id.gov_option);
        mBtnCreate = findViewById(R.id.btn_confirm);
        mBtnCreate.setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.THEME_GRADIENT_CORNER_BG));
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        findViewById(R.id.btn_quit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBtnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mGameOptionView.verifyOptionSelection()) {
                    return;
                }
                if (mEdtIntroduce.getText().length() == 0) {
                    Toasty.showNormal(R.string.room_introduce_hint);
                    return;
                }
                createGameRoom();
            }
        });
    }

    /**
     * 创建房间
     */
    private void createGameRoom() {
        showLoading(R.string.submit_data_doing);
        AddGameRoomRequest request = new AddGameRoomRequest();
        request.setUserId(RcUser.getInstance().getUserId());
        request.setBetNum(mGameOptionView.getGameBetAmountPair().second);
        request.setPlayerRestrict(mGameOptionView.getGamePlayerLimitPair().second);
        request.setGamePattern(mGameOptionView.getGameModePair().second);
        request.setIntroduce(mEdtIntroduce.getText().toString());
        GameCategory gameCategory = mGameOptionView.getGameCategory();
        request.setGameId(gameCategory.getGamePkId());
        request.setGameName(gameCategory.getGameName());
        request.setGameUrl(gameCategory.getGameUrl());
        OkRequest.getInstance().withUrl(UrlConfig.ADD_GAME_ROOM)
                .withRequest(request).doRequest(GameCreateActivity.class.getSimpleName(), new OkCallback<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse response, String srcData) {
                dismissLoading();
                if (response.isSuccess()) {
                    Toasty.showNormal(response.getMessage());
                    EventBus.getDefault().post(new RefreshEvent(DataType.REFRESH_GAME_ROOM_CREATED));
                    finish();
                } else {
                    Toasty.showNormal(response.getMessage());
                }
            }
        });
    }

}
